<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Category\StoreCategoryRequest;
use App\Http\Requests\Category\UpdateCategoryRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Model\Category;


class CategoryController extends Controller
{
    /**
     * 构造函数
     * CategoryController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xCate' => 'am-in',
            'categories' => Category::get_categories(),
            '_category' => 'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        return view('Admin.xHotel.category.index');

    }

    /**
     * 新增显示页
     */
    public function create()
    {

        return view('Admin.xHotel.category.create');
    }

    /**
     * 新增
     * @param StoreCategoryRequest $request
     */
    public function store(StoreCategoryRequest $request)
    {
        Category::create($request->all());
        Category::clear();
        return redirect('/category')->with('success', '添加成功');
    }

    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $data = array();
        $category = Category::find($id);
        $is_parent = Category::where('id', $category['parent_id'])->get()->toArray();



        $data['data'] = empty($is_parent) ? true : false;
        Category::clear();
        return view('Admin.xHotel.category.edit')->with('category', $category)->with('data', $data);
    }

    /**
     * 修改
     * @param UpdateCategoryRequest $request
     * @param $id
     */
    public function update(UpdateCategoryRequest $request, $id)
    {

        $category = Category::find($id);
        $category->update($request->all());
        Category::clear();
        return redirect('/category')->with('success', '编辑成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {

        if (!Category::check_children($id)) {
            return back()->with('error', '当前分类有子分类，请先将子分类删除后再尝试删除~');
        }

        if (!Category::is_hotel($id)) {
            return back()->with('error', '当前分类有图文，请先将图文删除后再尝试删除~');
        }

        Category::destroy($id);
        Category::clear();
        return back()->with('success', '删除成功');

    }

    /**
     * 是否显示
     * @param Request $request
     */
    public function is_something(Request $request)
    {
        $attr = $request->attr;
        $category = Category::find($request->id);
        $value = $category->$attr ? false : true;
        $category->$attr = $value;
        Category::clear();
        $category->save();
    }

    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $sort_order = $request->sort_order;
        $category = Category::find($request->id);
        $category->sort_order = $sort_order;
        Category::clear();
        $category->save();
    }


}
